home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************************
- * Boyer-Moore String Matching Algorithm *
- * *
- * Turbo-C version for MS-DOS *
- * adapted from UNIX source code *
- * *
- * Uses Boyer-Moore search algorithm. *
- * *
- * For help execute the program without parameters. *
- * *
- * Multiple file specifications may be made on the command line. *
- * *
- * This was compiled with the tiny memory model and then EXE2BIN *
- * was used on the output. The small memory model could just *
- * as easily been used. *
- * *
- * David Polansky *
- * 3-21-88 *
- * *
- *******************************************************************************/
- #include <stdio.h>
- #include <stdlib.h> /* N2 03-17-91 */
- #include <fcntl.h>
- #include <string.h>
- #include <dir.h>
- #include <io.h>
- #include <dos.h>
- #include <conio.h> /* N2 03-01-91 */
- #include "bm.h"
- #include "extern.h"
- #include "proto.h" /* N2 04-05-91 */
-
- #define TRUE 1
- #define FALSE 0
- #define true 1
- #define false 0
-
- char s_path[80]; /* source path */
- char s_drv[3]; /* drive */
- char s_dir[66]; /* directory */
- char s_file[9]; /* filename */
- char s_ext[5]; /* file ext */
-
- struct ffblk fcb; /* where our file names will be stored */
- int scrnlines; // 11-28-91 count lines displayed
- char showfname; // flag for filename
-
- unsigned _stklen = 6144;
-
- int main(int argc, char **argv) /* N2 06-30-91 */
- {
- char BigBuff[MAXBUFF + 2];
- /* We leave one extra character at the beginning and end of the buffer,
- * but don't tell Execute about it. This is so when someone is
- * scanning the buffer and scans past the end (or beginning)
- * we are still technically in the buffer (picky, but there ARE
- * machines which would complain) */
-
- int ret = 1; /* return code from Execute */
- int NPats; /* number of patterns to search for */
- char *FlagPtr; /* pointer to the flags */
- char c;
- int i; /* index of argument list */
- int TextFile; /* file to search */
- struct PattDesc *DescVec[MAXPATS];
- int first_time_through = TRUE; /* whether to use findfirst or findnext */
- char *PatternFile;
- char *Pattern;
-
- mFlag = TRUE;
- if (argc < 2) PutUsage(); // 11-28-91 this will exit!
- for(i = 1; i < argc; i++)
- {
- if ((argv[i][0] != '-') || (argv[i][1] == '\0')) break; // 11-28-91
- FlagPtr = argv[i];
- while(c = (char)*++FlagPtr) // while c != \0x0 do *??
- {
- c = tolower(c); /* 11-28-91 accept upper case switches */
- switch(c)
- {
- case 'm': mFlag = FALSE; break;
- case 'c': cFlag = 1; break;
- case 'f': fFlag = 1; /* read the patterns from a file */
- PatternFile = argv[++i];
- break;
- case 'e': eFlag = 1; /* get the patterns from next arg */
- Pattern = argv[++i];
- break;
- case 'l': lFlag = 1; break;
- case 'n': nFlag = 1; break;
- case 's': sFlag = 1; break;
- case 'x': xFlag = 1; break;
- case 'h': hFlag = 1; break;
- case 'z': zFlag = 1; break; // 11-28-91
- default:
- fprintf(stderr,"grep: invalid option: -%c\n",c); // 1.01
- PutUsage();
- break;
- } /* switch */
- } /* while */
- }
- /* argv[i] now points to patterns */
- if (!fFlag && !eFlag)
- {
- if (i > (argc - 1))
- {
- puts("grep: no pattern specified"); /* 11-28-91 */
- PutUsage();
- }
- else
- {
- NPats = MkDescVec(DescVec,argv[i]);
- ++i;
- }
- }
- else
- if (fFlag)
- {
- NPats = GetPatFile(PatternFile,DescVec);
- }
- else
- if (eFlag)
- {
- NPats = MkDescVec(DescVec,Pattern);
- }
- /* argv[i] now points to first file */
- if (i == argc)
- {
- /* use standard input, could be keyboard */
- ret &= Execute(DescVec,NPats,0,BigBuff+1);
- }
- else
- {
- scrnlines = wherey(); // 11-28-91 where did we start
- if(scrnlines >= 23) scrnlines = 1; // in case at end of screen
- for ( ;i < argc;i++)
- {
- first_time_through = TRUE;
- fnsplit(argv[i],s_drv,s_dir,s_file,s_ext); /* split the names */
- while (TRUE) /* a BREAK will end this loop */
- {
- if (first_time_through)
- {
- if (findfirst(argv[i],&fcb,0) == EOF)
- {
- fprintf(stderr,"No files matching %s\n",argv[i]);
- exit(2);
- }
- first_time_through = FALSE;
- }
- else
- {
- if (findnext(&fcb) == EOF)
- break; /* no more files to convert */
- }
- FileName = fcb.ff_name; // 11-28-91
- strcpy(s_path,s_drv);
- strcat(s_path,s_dir);
- strcat(s_path,fcb.ff_name);
- TextFile = _open(s_path,O_RDONLY);
- showfname = true; // reset showfname
- ret &= Execute(DescVec,NPats,TextFile,BigBuff+1);
- if (sFlag && !ret) exit(0);
- close(TextFile);
- }// while true
- }
- }
- if (cFlag) printf("%d\n",MatchCount);
- exit(0); // was ret
- return 0; // 04-01-92 N2
- } /* main */
-